package handler

import (
	"github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"
	jwt2 "github.com/golangblogs/gojwt"
	"github.com/google/uuid"
	"goods/api/request"
	"goods/consts"
	"goods/internal/dao"
	"goods/internal/model"
	"time"
)

func TokenOrders(c *gin.Context) {
	token, err := jwt2.NewJWT(consts.JWT_SECRET_KEY).CreateToken(jwt2.CustomClaims{
		ID: c.GetUint("userId"),
		StandardClaims: jwt.StandardClaims{
			ExpiresAt: time.Now().Unix() + 86400,
		},
	})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "token获取失败",
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": token,
	})
}

func CreateOrders(c *gin.Context) {
	var req request.CreateOrdersReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var user model.Users
	dao.GetOneByFields(&model.Users{Id: req.UsersId}, &user)
	if user.Id != req.UsersId {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "用户未登录",
			"data": nil,
		})
		return
	}
	var good model.Goods
	dao.GetOneByFields(&model.Goods{Id: req.GoodsId}, &good)
	if good.Id != req.GoodsId {
		c.JSON(200, gin.H{
			"code": 10002,
			"msg":  "商品不存在",
			"data": nil,
		})
		return
	}
	var order model.Orders
	dao.GetOneByFields(&model.Orders{OrderSn: req.OrderSn}, &order)
	if order.OrderSn != req.OrderSn {
		c.JSON(200, gin.H{
			"code": 10003,
			"msg":  "订单已存在",
			"data": nil,
		})
		return
	}
	s := uuid.NewString() + req.OrderSn
	orders := model.Orders{
		UsersId: req.UsersId,
		GoodsId: req.GoodsId,
		OrderSn: s,
		Num:     req.Num,
		Price:   req.Price,
	}
	if !dao.Create(&orders) {
		c.JSON(200, gin.H{
			"code": 10004,
			"msg":  "订单创建失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "订单创建成功",
		"data": orders,
	})
}

func QueryOrders(c *gin.Context) {
	var req request.QueryOrdersReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var user model.Users
	dao.GetOneByFields(&model.Users{Id: req.UsersId}, &user)
	if user.Id != req.UsersId {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "用户未登录",
			"data": nil,
		})
		return
	}
	var good model.Goods
	dao.GetOneByFields(&model.Goods{Id: req.GoodsId}, &good)
	if good.Id != req.GoodsId {
		c.JSON(200, gin.H{
			"code": 10002,
			"msg":  "商品不存在",
			"data": nil,
		})
		return
	}
	var orders []model.Orders
	dao.GetListByFields(&model.Orders{
		UsersId: req.UsersId,
		GoodsId: req.GoodsId,
	}, &orders)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": orders,
	})
}

func UpdateOrder(c *gin.Context) {
	var req request.UpdateOrderReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var user model.Users
	dao.GetOneByFields(&model.Users{Id: req.UsersId}, &user)
	if user.Id != req.UsersId {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "用户未登录",
			"data": nil,
		})
		return
	}
	var good model.Goods
	dao.GetOneByFields(&model.Goods{Id: req.GoodsId}, &good)
	if good.Id != req.GoodsId {
		c.JSON(200, gin.H{
			"code": 10002,
			"msg":  "商品不存在",
			"data": nil,
		})
		return
	}
	var order model.Orders
	dao.GetOneByFields(&model.Orders{
		Id: req.Id,
	}, &order)
	if order.Id != req.Id {
		c.JSON(200, gin.H{
			"code": 10003,
			"msg":  "订单不存在",
			"data": nil,
		})
		return
	}
	orders := model.Orders{
		Id:      req.Id,
		UsersId: req.UsersId,
		GoodsId: req.GoodsId,
		Status:  req.Status,
	}
	if !dao.Update(&orders) {
		c.JSON(200, gin.H{
			"code": 10003,
			"msg":  "修改失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "修改成功",
		"data": orders,
	})
}

func DeleteOrder(c *gin.Context) {
	var req request.DeleteOrderReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var user model.Users
	dao.GetOneByFields(&model.Users{Id: req.UsersId}, &user)
	if user.Id != req.UsersId {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "用户未登录",
			"data": nil,
		})
		return
	}
	var good model.Goods
	dao.GetOneByFields(&model.Goods{Id: req.GoodsId}, &good)
	if good.Id != req.GoodsId {
		c.JSON(200, gin.H{
			"code": 10002,
			"msg":  "商品不存在",
			"data": nil,
		})
		return
	}
	var order model.Orders
	dao.GetOneByFields(&model.Orders{
		Id: req.Id,
	}, &order)
	if order.Id != req.Id {
		c.JSON(200, gin.H{
			"code": 10003,
			"msg":  "订单不存在",
			"data": nil,
		})
		return
	}
	orders := model.Orders{
		Id:      req.Id,
		UsersId: req.UsersId,
		GoodsId: req.GoodsId,
	}
	if !dao.Delete(&orders) {
		c.JSON(200, gin.H{
			"code": 10004,
			"msg":  "删除失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
		"data": nil,
	})
}
